package cn.hd.controller;

import cn.hd.entity.*;
import cn.hd.service.FileuploadSevice;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class FileuploadController {
    @Autowired
    private FileuploadSevice fileuploadSevice;

    //多条件分页查询
    @RequestMapping("/selectAllFenYeFileupload")
    public Object selectAllFenYeFileupload(int pageNum,int pageSize,String paper){
        Map map=new HashMap();
        map.put("paper",paper);
        PageInfo<Fileupload> page = fileuploadSevice.selectAllFenYeFileupload(map, pageNum, pageSize);
        if(page.getList().size()!=0){
            return new Result("200","查询成功！",page);
        }else {
            return new Result("400","查询失败！",null);
        }
    }

    //添加资质文件
    @RequestMapping("/addFileupload")
    public Result addFileupload(Fileupload fileupload){
        int i = fileuploadSevice.addFileupload(fileupload);
        if(i!=0){
            return new Result("200","添加成功！",null);
        }else{
            return new Result("400","添加失败！",null);
        }
    }

    //根据编号查询资质文件
    @RequestMapping("/selectFileuploadById")
    public Object selectFileuploadById(long fileid){
        Fileupload fileupload = fileuploadSevice.selectFileuploadById(fileid);
        if(fileupload!=null){
            return new Result("200","查询成功！",fileupload);
        }else {
            return new Result("400","查询失败！",null);
        }
    }

    //修改资质文件
    @RequestMapping("/updateFileupload")
    public Result updateFileupload(Fileupload fileupload){
        int i = fileuploadSevice.updateFileupload(fileupload);
        if(i!=0){
            return new Result("200","修改成功！",null);
        }else{
            return new Result("400","修改失败！",null);
        }
    }

    //删除资质文件
    @RequestMapping("/deleteFileupload")
    public Result deleteFileupload(long fileid){
        FileuploadLicense fileuploadLicense = fileuploadSevice.selectFileuploadLicenseByFid(fileid);
        if(fileuploadLicense==null){
            int i = fileuploadSevice.deleteFileupload(fileid);
            if(i!=0){
                return new Result("200","删除成功！",null);
            }else{
                return new Result("400","删除失败！",null);
            }
        }else{
            return new Result("400","已被占用，不可被删除！",null);

        }

    }

}
